//
//  CRMFollowHistoryVC.m
//  New_Runway
//
//  Created by NewLife on 2018/3/9.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CRMFollowHistoryVC.h"
#import "CRMFollowHistoryCell.h"
#import "CRMFollowHistoryModel.h"

@interface CRMFollowHistoryVC ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) CRMFollowHistoryModel *model;

@end

@implementation CRMFollowHistoryVC
{
    UITableView *_tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBarView.titleLabel.text = @"跟进历史";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    
    [self loadData];
}

- (void)setupUI {
    
    _tableView = DPTableViewForStyle(UITableViewStylePlain)
    .setBackgroundColor([UIColor whiteColor])
    .setDelegate(self)
    .setDataSource(self)
    .setTableFooterView(DPView())
    .setTableHeaderView(DPView().setFrame(CGRectMake(0, 0, Screen_width, 8)).setBackgroundColor([UIColor whiteColor]))
    .registerCell(CRMFollowHistoryCell.class, @"cell")
    .setFrame(CGRectMake(0, self.navH, Screen_width, Screen_height - self.navH));
    
    self.view.addSubview(_tableView);
}

- (void)loadData {
    
    if (isEmptyOrNilString(self.customerShopId)) {
        [SVProgressHUD showErrorWithStatus:@"customerShopId不能为空" dismissDelay:1];
        return;
    }
    
    [SVProgressHUD show];
    [YQNetworking getWithUrl:[NSString stringWithFormat:@"%@%@", CRMContextApi, getFollowHistory] refreshRequest:NO cache:NO params:@{@"customerShopId": self.customerShopId} progressBlock:nil successBlock:^(id response) {
        [SVProgressHUD dismiss];
        self.model = [CRMFollowHistoryModel mj_objectWithKeyValues:response];
        
        if (self.model.code == 1) {
            
            [_tableView reloadData];
        } else {
            [SVProgressHUD showErrorWithStatus:self.model.msg dismissDelay:1];
        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
        [SVProgressHUD dismiss];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.model.result.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CRMFollowHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.model = self.model.result[indexPath.row];
    
    cell.showLineView = !(indexPath.row == self.model.result.count - 1);
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.model.result[indexPath.row].followState == 50) {
        return 150;
    }
    return 90;
}

@end
